package eu.jba.denshijisho;

import java.util.Date;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import eu.jba.denshijisho.dao.CardDao;
import eu.jba.denshijisho.dao.StatDao;
import eu.jba.denshijisho.entity.Card;
import eu.jba.denshijisho.entity.Stat;
import eu.jba.denshijisho.exceptionhandler.ExceptionHandler;
import eu.jba.denshijisho.listeners.ListenersHelper;

public class CreateNewActivity extends AbstractDenshiActivity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Thread.setDefaultUncaughtExceptionHandler( new ExceptionHandler( this ));
		setContentView(R.layout.activity_create_new);
		setFinishOnTouchOutside(false);
		final Spinner spinner = (Spinner)findViewById(R.id.word_phrase_spinner_create_new);
		final ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
			this, 
			R.array.word_phrase, 
			R.layout.dropdown_item
		);
		adapter.setDropDownViewResource(R.layout.dropdown_item);
		spinner.setAdapter(adapter);
		spinner.setOnItemSelectedListener( ListenersHelper.getSelectType() );
	}

	public void closeDialog(View view) {
		setResult(RESULT_CANCELED);
		finish();
	}

	public void saveCard(View view) {
		final String svkLang = ((EditText) findViewById(R.id.svk_lang_text_field_create_new))
				.getText().toString();
		final String kana = ((EditText) findViewById(R.id.kana_text_field_create_new))
				.getText().toString();
		final String kanji = ((EditText) findViewById(R.id.kanji_text_field_create_new))
				.getText().toString();
		final String type = ListenersHelper.getSelectedType();
		final Card newCard = new Card(svkLang, kana, kanji, type, null);

		if( checkBeforeSave(newCard) > 0 ){
			new AlertDialog.Builder( this ).setMessage("Similar words found do you really want to save?")
			.setCancelable(false).setPositiveButton("Yes", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					newCard.setStat(getStat());
					saveCard(newCard);
					setResult(RESULT_OK);
					finish();
				}
			}).setNegativeButton("No", null).show();
		}else{
			newCard.setStat(getStat());
			saveCard(newCard);
			setResult(RESULT_OK);
			finish();			
		}
	}
	
	private void saveCard(Card card){
		CardDao adapter = new CardDao(this);
		adapter.open();
		adapter.insert(card);
		adapter.close();
	}
	
	private int checkBeforeSave(Card newCard){
		final CardDao cardDao = new CardDao(this);
		cardDao.open();
		int i = cardDao.findSimilar(newCard.getSvkWord(), newCard.getKanaWord(), newCard.getKanji());
		cardDao.close();
		return i;
	}
	
	public Stat getStat(){
		Stat stat = new Stat(new Date(), new Date(), 0, 0, 0, 0, 2.5);
		StatDao statDao = new StatDao( this );
		statDao.open();
		long id = statDao.insert(stat);
		stat.setId((int) id);
		statDao.close();
		return stat;
	}
}
